package com.hotel.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hotel.bean.Regist;
import com.hotel.service.UserService;
import com.hotel.service.impl.UserServiceImpl;
import com.hotel.util.BaseServlet;
import com.hotel.util.CustomBeanUtils;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	
	
       
	public String registUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "/user/regist.jsp";
	}

	public String loginUi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "/user/login.jsp";
	}
	
	
	//注册 王俊程  
	public String regist(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 接受页面的数据
		Map<String, String[]> parameterMap = request.getParameterMap();
		

		Regist user = CustomBeanUtils.populate(Regist.class, parameterMap);

		UserService service = new UserServiceImpl();
		boolean success = service.regist(user);
		if (success) {
			request.getSession().setAttribute("pname", user.getPname());
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=loginUi");
		} else {
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=registUi");
		}

		return null;
	}
	
	//登录 常开瑞
	public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接受页面的数据
		Map<String, String[]> parameterMap = request.getParameterMap();
		Regist user = CustomBeanUtils.populate(Regist.class, parameterMap);
		// 自动登录
		String autologin = request.getParameter("autologin");
		
		// 调用service层进行登录判断
		UserService userService = new UserServiceImpl();	
		Regist loginUser = userService.login(user);

		if (loginUser != null) {// 登陆成功
			// 创建Cookie
			Cookie cookie = new Cookie("pid", String.valueOf(loginUser.getPid()));
			// 设置Cookie时效及作业范围
			cookie.setMaxAge(30); // 单位是秒
			cookie.setPath(request.getContextPath()); // 整个应用有效
			// 传递给客户端
			response.addCookie(cookie);

			request.getSession().setAttribute("user", loginUser);
			response.sendRedirect(request.getContextPath() + "/HomeServlet?action=ui");
		} else {
			// 登录失败
			response.sendRedirect(request.getContextPath() + "/UserServlet?action=loginUi");
		}
		return null;
	}

	public String logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pid = null;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("pid") && cookie.getMaxAge() > 0) {
					cookie.setMaxAge(0);
				}
			}
		}

		request.getSession().removeAttribute("user");
		response.sendRedirect(request.getContextPath() + "/HomeServlet?action=ui");
		return null;

	}

}




